package hn.cch.configuration;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ApplicationConfiguration {

    @Value("${custom.parameter:default-value}")
    private String parameter;

    @Value("${custom.directory}")
    private String directory;

    public String getParameter() {
        return parameter;
    }

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    public String getDirectory() {
        return directory;
    }

    public void setDirectory(String directory) {
        this.directory = directory;
    }

    /**
     * 自定义线程池
     * @return ThreadPoolTaskExecutor
     */
    @Bean(value = "taskExecutor")
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        // 线程名称前缀
        threadPoolTaskExecutor.setThreadNamePrefix("async-");
        // 线程池核心线程数
        threadPoolTaskExecutor.setCorePoolSize(2);
        // 线程池最大线程数
        threadPoolTaskExecutor.setMaxPoolSize(4);
        // 线程存活时间
        threadPoolTaskExecutor.setKeepAliveSeconds(60);
        // 队列最大容量
        threadPoolTaskExecutor.setQueueCapacity(10);
        // 设置拒绝策略
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 单独使用要初始化，使用Spring管理Bean对象自动调用
        // threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }
}
